package FINAL.THUNGHIEM;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JSeparator;

public class ThuNghiem1 extends JFrame {

	private JPanel contentPane;
	private final JButton btnChnFile = new JButton("Chọn file");
	JFileChooser chooser = new JFileChooser("E:/DATA");
	File file;
	private final JLabel label = new JLabel("");
	private final JLabel lblNode = new JLabel("Node:");
	private final JTextField textField = new JTextField();
	private final JSeparator separator = new JSeparator();
	private final JButton btnInKtQu = new JButton("In kết quả");
	private final JLabel lblStt = new JLabel("STT:");
	private final JLabel lblKhongCch = new JLabel("Khoảng cách:");
	private final JLabel kq1 = new JLabel("");
	private final JLabel kq2 = new JLabel("");

	List<Integer> list_node = new ArrayList<Integer>();
	List<Integer> list_kc = new ArrayList<Integer>();

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ThuNghiem1 frame = new ThuNghiem1();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public ThuNghiem1() {
		textField.setBounds(109, 42, 86, 20);
		textField.setColumns(10);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		btnChnFile.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				int flag = chooser.showOpenDialog(ThuNghiem1.this);
				if (flag == JFileChooser.APPROVE_OPTION) {
					file = chooser.getSelectedFile();
					label.setText(file.getAbsolutePath());
					list_kc.clear();
					list_node.clear();
				}
			}
		});
		btnChnFile.setBounds(10, 11, 89, 23);

		contentPane.add(btnChnFile);
		label.setBounds(109, 15, 315, 14);

		contentPane.add(label);
		lblNode.setBounds(10, 45, 46, 14);

		contentPane.add(lblNode);

		contentPane.add(textField);
		separator.setBounds(10, 102, 402, 2);

		contentPane.add(separator);
		btnInKtQu.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if (file == null) {
					JOptionPane.showMessageDialog(null, "Chưa chọn file");
					return;
				}
				// ////////////////////////đọc file///////////////////////////
				BufferedReader br = null;
				try {
					br = new BufferedReader(new FileReader(file));
					String line1, line2;
					line1 = br.readLine();
					line2 = br.readLine();
					while (true) {
						int index = line1.indexOf('\t');
						// System.out.println(line1.substring(0, index));
						list_node.add(Integer.parseInt(line1
								.substring(0, index)));
						line1 = line1.substring(index + 1);
						if (line1.isEmpty()) {
							System.out.println("xong dòng 1");
							break;
						}
					}
					int count=0;
					while (true) {
						int index = line2.indexOf('\t');
						// System.out.println(line2.substring(0, index));
						list_kc.add(Integer.parseInt(line2.substring(0, index)));
						if(Integer.parseInt(line2.substring(0, index))==-1){
							count++;
						}
						line2 = line2.substring(index + 1);
						if (line2.isEmpty()) {
							System.out.println("xong dòng 2: "+(7500-count));
							break;
						}
					}
				} catch (Exception e) {

				}// ////////////////////////////////////////////////
				int node = Integer.parseInt(textField.getText());
				kq1.setText("" + list_node.indexOf(node));
				kq2.setText("" + list_kc.get(list_node.indexOf(node)));
			}
		});
		btnInKtQu.setBounds(10, 118, 89, 23);

		contentPane.add(btnInKtQu);
		lblStt.setBounds(10, 160, 46, 14);

		contentPane.add(lblStt);
		lblKhongCch.setBounds(10, 185, 89, 14);

		contentPane.add(lblKhongCch);
		kq1.setBounds(126, 160, 69, 14);

		contentPane.add(kq1);
		kq2.setBounds(126, 185, 69, 14);

		contentPane.add(kq2);
	}
}
